package com.wang.lotterysystem.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wang.lotterysystem.common.pojo.CommonResult;
import com.wang.lotterysystem.controller.param.prize.CreatePrizeParam;
import com.wang.lotterysystem.controller.param.PageListParam;
import com.wang.lotterysystem.controller.result.prize.FindPrizeListResult;
import com.wang.lotterysystem.service.PictureService;
import com.wang.lotterysystem.service.PrizeService;
import com.wang.lotterysystem.service.dto.PageListDTO;
import com.wang.lotterysystem.service.dto.prize.PrizeDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

@Tag(name = "奖品相关功能接口")
@RestController
@Slf4j
public class PrizeController {
    @Autowired
    private PictureService pictureService;
    @Autowired
    private PrizeService prizeService;
    @Autowired
    private ObjectMapper objectMapper;

    @Operation(summary = "奖品图片上传")
    @RequestMapping("/pic-upload")
    public CommonResult<String> picUpload(@RequestPart("file") @NotNull MultipartFile file){
        //打印日志
        log.info("上传图片:{}",file.getOriginalFilename());
        //调用Service层的savePicture方法, 将结果转为CommonResult并返回
        return CommonResult.success(pictureService.savePicture(file));
    }

    @Operation(summary = "创建奖品")
    @RequestMapping("/prize/create")
    public CommonResult<Long> createPrize(@Validated @RequestPart("param") CreatePrizeParam prizeParam,
                                          @RequestPart("prizePic") MultipartFile file) throws JsonProcessingException {
        //打印日志
        log.info("create prize:{}",objectMapper.writeValueAsString(prizeParam));
        //调用Service层的createPrize方法, 将结果转为CommonResult并返回
        return CommonResult.success(prizeService.createPrize(prizeParam,file));
    }

    @Operation(summary = "获取奖品列表")
    @RequestMapping("/prize/find-list")
    public CommonResult<FindPrizeListResult> findPrizeList(PageListParam param) throws JsonProcessingException {
        //打印日志
        log.info("findPrizeList param:{}",objectMapper.writeValueAsString(param));
        //调用Service层的findPrizeList方法
        PageListDTO<PrizeDTO> pageListDTO = prizeService.findPrizeList(param);
        //将结果转为CommonResult并返回
        return CommonResult.success(convertToFindPrizeListResult(pageListDTO));
    }

    private FindPrizeListResult convertToFindPrizeListResult(PageListDTO<PrizeDTO> pageListDTO) {
        FindPrizeListResult result = new FindPrizeListResult();
        result.setTotal(pageListDTO.getTotal());
        List<FindPrizeListResult.PrizeInfo> prizeInfoList = new ArrayList<>();
        for (PrizeDTO prizeDTO : pageListDTO.getRecords()) {
            FindPrizeListResult.PrizeInfo prizeInfo = new FindPrizeListResult.PrizeInfo();
            prizeInfo.setPrizeName(prizeDTO.getName());
            prizeInfo.setPrizeId(prizeDTO.getId());
            prizeInfo.setDescription(prizeDTO.getDescription());
            prizeInfo.setImageUrl(prizeDTO.getImageUrl());
            prizeInfo.setPrice(prizeDTO.getPrice());
            prizeInfoList.add(prizeInfo);
        }
        result.setRecords(prizeInfoList);
        return result;
    }
}